<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-24 15:09:21
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 加料单
 */

namespace App\Http\Services\Finance;


use App\Http\Services\Service;
use App\Repositories\FinanceAcceptanceRecRepository;

/**
 * @property FinanceAcceptanceRecRepository $repository 
 */
class AcceptanceRecService extends Service
    {
    public function boot(FinanceAcceptanceRecRepository $repository)
        {
        $this->repository = $repository;
        }

    public function info()
        {
        return $this->repository->whereId(request("id"))->getOne();
        }
    public function list()
        {
        $data = $this->repository->with(['customer' => ['billtrade']])->condition()->getGrid();
        return $data;
        }
    protected function save()
        {

        $data = request()->toArray();
        return $this->repository->validate($data)->autosave($data);

        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($gdata)
            {
            if ($gdata->status_id != 0) {
                fail("单据[{$gdata->status->name}]！不能审核！");
                } 
            return $gdata->finance();
            });
        }
    
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("单据[{$v->status->name}]！禁止删除");
                }
            return $v->delete();
            });
        }

    protected function debtor()
        {
        $request = request();

        $gdata               = $this->repository->whereId($request->id)->first(); 
        $gdata->_leftamount  = $gdata->leftamount;
        $gdata->leftamount   = 0;
        $gdata->account_id   = $request->account_id;
        $gdata->usedate      = $request->usedate;
        $gdata->debtorremark = $request->debtorremark;
        $gdata->save();
        return $gdata->debtFinace();
        } 
    }